#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Shu Yang Wang
# Date: 2017/11/7
from django.conf import settings
from rbac import models

'''
初始化权限，获取当前用户权限并添加到session中
'''

def initial_permission(request, user):
    '''
    初始化权限
    :param request: 请求对象
    :param user: 当前用户对象 
    :return: 
    '''
    # 1.获得角色对应权限表数据，并去重
    permission_list = user.roles.values(
        'permissions__id',
        'permissions__caption',
        'permissions__url',
        'permissions__code',
        'permissions__is_menu',
        'permissions__group__id',
        'permissions__group__is_group',
        'permissions__group__parent_id',
    ).distinct()

    '''
    2、格式化code,url转化为{{permissions__group_id:{codes:[], urls:[]}}}格式，
    菜单格式转化为[{'caption':'',url:'',menu_id:'',menu_caption:''}]格式，
    '''
    permission_dict = {}
    permission_menu_list = []
    for item in permission_list:
        code = item['permissions__code']
        group_id = item['permissions__group__id']
        url = item['permissions__url']

        if group_id in permission_dict:
            permission_dict[group_id]['codes'].append(code)
            permission_dict[group_id]['urls'].append(url)
        else:
            permission_dict[group_id] = {'codes': [code, ], 'urls': [url, ]}

        is_menu = item['permissions__is_menu']
        if is_menu:
            tmp = {
                'permissions__id': item['permissions__id'],
                'permissions__caption': item['permissions__caption'],
                'permissions__url': item['permissions__url'],
                'permissions__parent_id': item['permissions__group__parent_id']
            }
            permission_menu_list.append(tmp)
    # 3、写入权限和菜单列表到session中
    request.session[settings.RBAC_PERMISSION_URL_SESSION_KEY] = permission_dict
    # 菜单列表
    menu_list = list(models.Memu.objects.filter(is_group=False).values('id', 'caption', 'parent_id'))
    request.session[settings.RBAC_MENU_PERMISSION_SESSION_KEY] = {
        settings.RBAC_MENU_KEY: menu_list,
        settings.RBAC_MENU_PERMISSION_KEY: permission_menu_list,
    }

    '''
       permission_dict= {4: 
           {'codes': ['manger', 'server'], 
           'urls': ['/manger/', '/server/']}, 
        5: {'codes': ['monitor', 'project-list', 'project', 'webshell'], 
           'urls': ['/monitor/', '/project/list/', '/project/', '/WebShell/']}, 
        3: {'codes': ['ser-permission', 'project-ser_pro'], 
           'urls': ['/manger/permission', '/project/ser_pro/']}}

       menu_list=[
           {'id': 6, 'caption': '服务器配置', 'parent_id': None}, 
           {'id': 7, 'caption': '资产列表', 'parent_id': 6}, 
           {'id': 8, 'caption': '监控项配置', 'parent_id': 6}, 
           {'id': 9, 'caption': '服务器组权限', 'parent_id': 6}, 
           {'id': 10, 'caption': 'WebShell', 'parent_id': 6}, 
           {'id': 11, 'caption': '业务线配置', 'parent_id': None}, 
           {'id': 12, 'caption': '项目列表', 'parent_id': 11}, 
           {'id': 13, 'caption': '项目与服务器关系', 'parent_id': 11}
       ]
       permission_menu_list=[
           {'permissions__id': 1, 'permissions__caption': '服务器管理权限', 'permissions__url': '/manger/', 'permissions__parent_id': 6}, 
           {'permissions__id': 3, 'permissions__caption': '监控项配置', 'permissions__url': '/monitor/', 'permissions__parent_id': 11}, 
           {'permissions__id': 7, 'permissions__caption': '项目列表', 'permissions__url': '/project/list/', 'permissions__parent_id': 11}, 
           {'permissions__id': 6, 'permissions__caption': '服务器组权限', 'permissions__url': '/manger/permission', 'permissions__parent_id': 6}, 
           {'permissions__id': 8, 'permissions__caption': '项目与服务器关系', 'permissions__url': '/project/ser_pro/', 'permissions__parent_id': 6}, 
           {'permissions__id': 2, 'permissions__caption': '资产列表', 'permissions__url': '/server/', 'permissions__parent_id': 6}, 
           {'permissions__id': 5, 'permissions__caption': '业务线配置', 'permissions__url': '/project/', 'permissions__parent_id': 11}, 
           {'permissions__id': 4, 'permissions__caption': 'WebShell', 'permissions__url': '/WebShell/', 'permissions__parent_id': 11}
       ]
       '''